
package system.base;

import framework.core.*;
import system.SelectManager;
import system.data.*;

/*
	Author    :Wing
	CreateTime:2008-01-30 14:11:28
*/
public class Customer extends CustomerBase 
{

//----system generated start----// 	
public Customer() 
{
	
}

private static void delete(DBConnection con,String id) throws Exception
{
	Conditions conditions = new Conditions();

	conditions.add(ID,id);
	
	delete(con,TABLE_NAME,conditions);
}
public static Customer fetch()
{
	return new Customer();
}
public static Customer copy(Customer srcObj)
{
	Customer obj = fetch();
	obj.copyBase(srcObj);
	return obj;
}
public static Customer fetch(DBConnection con,String id) throws Exception
{
	Conditions conditions = new Conditions();

	conditions.add(ID,id);
	
	return fetch(con,conditions);
}
public static Customer fetch(DBConnection con,Conditions conditions) throws Exception
{
	DBResult result = null;
	try
	{
		result = DBEngine.doSelect(con,"select * from " + TABLE_NAME,conditions);
		java.sql.ResultSet rs = result.getRs();
		Customer obj = null;
		if(rs.next())
		{
			obj = fetch(rs);
		}
		return obj;
	}
	finally
	{
		if(result!=null) result.clear() ;
	}
}
public static Customer fetch(java.sql.ResultSet rs) throws Exception
{
	Customer obj = new Customer();
	obj.initObject(rs);
	return obj;
}
protected void getUnique(DBConnection con) throws Exception
{
		setId(DBEngine.getId(con));
}
//----system generated end----//



//----add by Wing start----//
public static final String PRIX = "00";

public String viewIsBalance(DBConnection con) throws Exception
{
  return SelectManager.viewYesOrNo(getIsBalance()+"");   
} 

public String viewIsDraw(DBConnection con) throws Exception
{
  return SelectManager.viewYesOrNo(getIsDraw()+"");   
} 

public String viewStatus(DBConnection con) throws Exception
{
  return SelectManager.viewValidOrNo(getStatus()+""); 
}

private void checkUnique(DBConnection con) throws Exception 
{
    Conditions cs = new Conditions();
    cs.add(CODE, getCode());
    cs.add(ID, Conditions.NOT_EQUAL, getId());
    if(DBEngine.doCount(con, "select count(id) from "+TABLE_NAME, cs) > 0)
        throw new BizException("已存在的编码，请重新填写。");
}
public void beforeUpdate(DBConnection con) throws Exception
{
    checkUnique(con);
    setLastUpdateTime(new DateUtil().getFullString());      
}
public void beforeInsert(DBConnection con) throws Exception
{
    checkUnique(con);
    DateUtil du = new DateUtil();
    setCreateTime(du.getFullString());
    setLastUpdateTime(du.getFullString());
}  

//获取系统内所有有效客户
public static ObjectList fetchAllOrgs(DBConnection con) throws Exception
{
      Values vs = new Values();
      DBEngine.checkCon(con);
      String sql = "select * from " + Customer.TABLE_NAME + " where " + Customer.CODE + " like ? order by " + Customer.CODE;
      vs.add(PRIX+"%");
      
      DBResult result = DBEngine.doSelect(con,sql,vs);
      
      ObjectList list = ObjectList.fromRs(Customer.class,result.getRs());
      result.clear();
      return list;    
}

//获取当前客户及下属的所有客户
public static ObjectList fetchAllOrgs(DBConnection con, String currCustomerCode) throws Exception
{
      Values vs = new Values();
      DBEngine.checkCon(con);
      String sql = "select * from " + Customer.TABLE_NAME + " where " + Customer.CODE + " like ? order by " + Customer.CODE;
      vs.add(currCustomerCode+"%");
      
      DBResult result = DBEngine.doSelect(con,sql,vs);
      
      ObjectList list = ObjectList.fromRs(Customer.class,result.getRs());
      result.clear();
      return list;    
}

public static ObjectList fetchAllOrgs(DBConnection con, String currCustomerCode, String warehouseCode) throws Exception
{
      Values vs = new Values();
      DBEngine.checkCon(con);
      String sql = "select * from " + Customer.TABLE_NAME + " where " + Customer.WAREHOUSE_CODE + " like ? order by " + Customer.CODE;
      vs.add(warehouseCode+"%");
      
      DBResult result = DBEngine.doSelect(con,sql,vs);
      
      ObjectList list = ObjectList.fromRs(Customer.class,result.getRs());
      result.clear();
      return list;    
}

//获取当前编码的最后两位有效编码
public static String getValidCard(String currCode){
  try{
      if (currCode!= null){
          int temp = currCode.length()/PRIX.length()-1;
          if (temp > 0) {
              currCode = currCode.substring(PRIX.length()*temp);
          }
      }   
      return currCode;
  }catch(Exception e){
      return "";
  }
}

//通过客户编码获取客户实例
public static Customer fetchByCode(DBConnection con,String code) throws Exception
{
    Conditions conditions = new Conditions();

    conditions.add(CODE,code);
  
    return fetch(con,conditions);
} 


//----add by Wing end----//

//----add by beny start----//
//重写仓库名称
public String viewWarehouseName(DBConnection con) throws Exception
{
    String mReturn = "";
    Warehouse w = Warehouse.fetchByCode(con, getWarehouseCode());
    if (w != null)
    {
        mReturn = w.getName();
    }
    return mReturn;
}
//----add by benny end----//

}

